package org.dandsoft.blackbox.laboratory.samples.lighter.view;

import org.dandsoft.blackbox.laboratory.samples.lighter.World;
import org.dandsoft.blackbox.laboratory.samples.lighter.Lighter;
import org.dandsoft.blackbox.laboratory.samples.lighter.Sun;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: dvilyuzhanin
 * Date: 23.03.2009 20:07:58
 */
public class WorldPanel extends JPanel {

    private World world;
    private JScrollPane scrollPane;
    private Canvas canvas;
    private boolean isWaitSetLighterPosition;

    public WorldPanel(World world) {
        this.world = world;
        setLayout(new BorderLayout());
        canvas = new Canvas();
        canvas.setMinimumSize(new Dimension(world.getWidth(), world.getHeight()));
        canvas.setMaximumSize(new Dimension(world.getWidth(), world.getHeight()));
        canvas.setPreferredSize(new Dimension(world.getWidth(), world.getHeight()));

        canvas.setSize(new Dimension(world.getWidth(), world.getHeight()));

        scrollPane = new JScrollPane(canvas,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(scrollPane, BorderLayout.CENTER);


    }

    public void waitSetLighterPosition() {
        isWaitSetLighterPosition = true;
    }

    class Canvas extends JPanel {

        Canvas() {

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (isWaitSetLighterPosition) {
                        isWaitSetLighterPosition = false;
                        world.getLighter().setX((int) e.getPoint().getX());
                        world.getLighter().setY((int) e.getPoint().getY());
                        canvas.repaint();
                    }
                }

            });
        }

        @Override
        public void paint(Graphics g) {
            Sun sun = world.getSun();
            Lighter lighter = world.getLighter();
            double x = sun.getX() - sun.getRange();
            double y = sun.getY() - sun.getRange();
            g.drawOval((int) x, (int) y, sun.getRange() * 2, sun.getRange() * 2);


            g.fillOval(lighter.getX(), lighter.getY(), 10, 10);
        }
    }

}
